package com.wormhole.wormholemanagement.controller;

import com.wormhole.wormholemanagement.entity.Operator;
import com.wormhole.wormholemanagement.service.OperatorService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/operators")
@Tag(name = "操作员管理")
public class OperatorController {

    private final OperatorService operatorService;

    @Autowired
    public OperatorController(OperatorService operatorService) {
        this.operatorService = operatorService;
    }

    @PostMapping
    @Operation(summary = "创建操作员")
    public ResponseEntity<Operator> createOperator(@RequestBody Operator operator) {
        Operator createdOperator = operatorService.createOperator(operator);
        return ResponseEntity.ok(createdOperator);
    }

    @PutMapping("/{id}")
    @Operation(summary = "更新操作员")
    public ResponseEntity<Operator> updateOperator(@PathVariable Long id,
                                                   @RequestBody Operator operator) {
        Operator updatedOperator = operatorService.updateOperator(id, operator);
        return ResponseEntity.ok(updatedOperator);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除操作员")
    public ResponseEntity<Void> deleteOperator(@PathVariable Long id) {
        operatorService.deleteOperator(id);
        return ResponseEntity.noContent().build();
    }

    @GetMapping("/{id}")
    @Operation(summary = "获取操作员详情")
    public ResponseEntity<Operator> getOperatorById(@PathVariable Long id) {
        Operator operator = operatorService.getOperatorById(id);
        return ResponseEntity.ok(operator);
    }

    @GetMapping("/username/{username}")
    @Operation(summary = "根据用户名获取操作员")
    public ResponseEntity<Operator> getOperatorByUsername(@PathVariable String username) {
        Operator operator = operatorService.getOperatorByUsername(username);
        return ResponseEntity.ok(operator);
    }

    @GetMapping
    @Operation(summary = "获取操作员列表")
    public ResponseEntity<Page<Operator>> getAllOperators(
            @PageableDefault(size = 10) Pageable pageable) {
        Page<Operator> operators = operatorService.getAllOperators(pageable);
        return ResponseEntity.ok(operators);
    }
}